package edu.yuhf.dbutil;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBConnection {
	private static final String URL;
	private static final String USERNAME;
	private static final String PASSWORD;
	private static final String CLASSNAME;
	
		static {
			Properties prop=new Properties();
			try {
				prop.load(DBConnection.class.getResourceAsStream("/jdbc.properties"));
			} catch (IOException e) {
				System.out.println("jdbc.properties file not fond,error message is"+e.getMessage());
			}
			URL=prop.getProperty("jdbc.url");
			USERNAME=prop.getProperty("jdbc.userName");
			PASSWORD=prop.getProperty("jdbc.password");
			CLASSNAME=prop.getProperty("jdbc.className");
			try {
				Class.forName(CLASSNAME);
				
			} catch (ClassNotFoundException e) {
				System.out.println("Class not fond,error message is"+e.getMessage());
			}
			
		}
		public static Connection getConnection() {
			Connection conn=null;
			try {
				conn=DriverManager.getConnection(URL,USERNAME,PASSWORD);
			} catch (SQLException e) {
				System.out.println("database connection error ,message is"+e.getMessage());
			
		}
			return conn;
}
		public static void closeConnection(Connection conn) {
			if(null!=conn) {
				try {
					conn.close();
				} catch (SQLException e) {
					System.out.println("database close error,message is "+e.getMessage());
				}
				
			}
		}
		
}